package com.qf.erp_system_admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.erp_system_admin.entity.Refund;
import com.qf.erp_system_admin.service.RefundService;
import com.qf.erp_system_admin.util.R;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * --- 一点都不难 ---
 *
 * @author DZW
 * @date 2024/7/26
 * @desc
 */

@RequestMapping("/refund/form")
@RestController
public class RefundController {
    private Logger logger = LogManager.getLogger(RefundController.class);
    @Autowired
    private RefundService refundService;


    /**
     * 退单页面的查询全部
     * @param map 分页信息以及模糊搜索信息
     * @return
     */
    @GetMapping("/findAll")
    public R findAll(@RequestParam HashMap<String, String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));

        // 1.开始分页
        PageHelper.startPage(pageNum, pageSize);

        List<Refund> list = refundService.findAll(map);

        // 2.获得全部分页信息
        PageInfo<Refund> pageInfo = new PageInfo<>(list);

        return R.ok(pageInfo);
    }

    /**
     * 退单页面的删除按钮
     * @param id 删除行的id
     * @return 是否删除成功
     */

    @GetMapping("/delOne")
    public R delById(int id){
        boolean isOk = refundService.delById(id);
        return R.ok(isOk);
    }


    /**
     * 退单页面的更新状态按钮
     * @param refund 前端发送的json对象
     * @return 是否更改成功
     */
    @PostMapping("/update")
    public R update(@RequestBody Refund refund){
        System.out.println("执行-----");
        boolean isOk = refundService.update(refund);
        return R.ok(isOk);
    }

    /**
     * 批量删除
     * @param ids id数组
     * @return 是否删除成功
     */
    @GetMapping("/delBatch")
    public R delBatch(@RequestParam List<Integer> ids){
        System.out.println("Controller接收前端的user = " + ids);
        boolean isOk = refundService.delBatch(ids);
        return R.ok(isOk);
    }
}
